//
//  BaseNavigationController.swift
//  OMTUIKitSwift
//
//  Created by 昭荣伊 on 2018/12/10.
//

import Foundation

public class BaseNavigationController: UINavigationController, UIGestureRecognizerDelegate, UINavigationControllerDelegate {

    public override func viewDidLoad() {
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    public override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.children.count >= 1 {
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return self.children.count > 1
    }
    // 返回回调处理
    public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        let tc = navigationController.topViewController?.transitionCoordinator
        tc?.notifyWhenInteractionEnds({ (context) in
            if context.isCancelled {
                return
            }
            let ctrl = context.viewController(forKey: .from)
            // 处理返回时的回调
            if ctrl?.responds(to: #selector(BaseViewController.excuteWillBackBlock)) ?? false {
                ctrl?.performSelector(onMainThread: #selector(BaseViewController.excuteWillBackBlock), with: nil, waitUntilDone: true)
            }
        })
    }
}
